Skip to content

[Pygments] Complete stubs for various modules#15610

Open
brianschubert wants to merge 6 commits intopython:mainfrom
brianschubert:stubs-pygments-complete-misc
Open

[Pygments] Complete stubs for various modules#15610
brianschubert wants to merge 6 commits intopython:mainfrom
brianschubert:stubs-pygments-complete-misc

Conversation

@brianschubert
Copy link
Copy Markdown
Collaborator

No description provided.

Comment on lines +70 to +71
def combine(*args: _Cats) -> str: ...
def allexcept(*args: _Cats) -> str: ...
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: these are always called with literal values in practice (example), so I think using a literal type here provides some useful validation (even if a little verbose in the stubs)

Comment on lines +41 to +42
def guess_decode_from_terminal(text: bytes, term: TextIO) -> tuple[str, str]: ...
def terminal_encoding(term: TextIO) -> str: ...
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't feel worth using a more precise protocol since term is always sys.stdin / a proper file object in practice

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on a quest to eliminate these pseudo-protocols (also see my comments on Mapping above). But TextIO is especially bad. A real protocol isn't really possible, until we get optional protocol members (python/typing#601). But I'd say even using Any here would be better, since these functions work with basically every object, unless encoding isn't a string field (unlikely).

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, a few questions/optional remarks.

Comment on lines +17 to +19
def format(tokens: Iterator[tuple[_TokenType, str]], formatter: Formatter[_T], outfile: SupportsWrite[_T]) -> None: ...
@overload
def format(tokens, formatter: Formatter[_T], outfile: None = None) -> _T: ...
def format(tokens: Iterator[tuple[_TokenType, str]], formatter: Formatter[_T], outfile: None = None) -> _T: ...
Copy link
Copy Markdown
Collaborator

@srittau srittau Apr 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these be iterables?

Suggested change
def format(tokens: Iterator[tuple[_TokenType, str]], formatter: Formatter[_T], outfile: SupportsWrite[_T]) -> None: ...
@overload
def format(tokens, formatter: Formatter[_T], outfile: None = None) -> _T: ...
def format(tokens: Iterator[tuple[_TokenType, str]], formatter: Formatter[_T], outfile: None = None) -> _T: ...
def format(tokens: Iterable[tuple[_TokenType, str]], formatter: Formatter[_T], outfile: SupportsWrite[_T]) -> None: ...
@overload
def format(tokens: Iterable[tuple[_TokenType, str]], formatter: Formatter[_T], outfile: None = None) -> _T: ...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arrgh, yes. Hasty tab completion strikes again

Comment on lines +18 to +26
def get_choice_opt(
options: Mapping[str, Any], optname: str, allowed: Container[_T], default: _T | None = None, normcase: bool = False
) -> _T: ...
def get_bool_opt(options: Mapping[str, Any], optname: str, default: bool | None = None) -> bool: ...
def get_int_opt(options: Mapping[str, Any], optname: str, default: int | None = None) -> int: ...

# Return type and type of 'default' depend on the signature of the function whose **kwargs
# are being processed.
def get_list_opt(options: Mapping[str, Any], optname: str, default: list[Any] | tuple[Any, ...] | None = None) -> list[Any]: ...
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that call these just call get() on options, I would prefer a simple protocol here instead of the heady-handed Mapping pseudo-protocol. In fact I think such a protocol would be useful to have in _typeshed. (Although we can't really do the latter in this PR, since stdlib changes need a bit of time to propagate through type checkers.)

Comment on lines +41 to +42
def guess_decode_from_terminal(text: bytes, term: TextIO) -> tuple[str, str]: ...
def terminal_encoding(term: TextIO) -> str: ...
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on a quest to eliminate these pseudo-protocols (also see my comments on Mapping above). But TextIO is especially bad. A real protocol isn't really possible, until we get optional protocol members (python/typing#601). But I'd say even using Any here would be better, since these functions work with basically every object, unless encoding isn't a string field (unlikely).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 5, 2026

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants